package com.pervasa.demo.re.impl.data;

import org.sensorplatform.actuators.servo.hs322.HS322Servo;

import com.pervasa.atlas.dev.service.AtlasService;
import com.pervasa.demo.re.impl.ReactivityEngine;

public class AtomicAction extends Action {
	private ReactivityEngine env;
	
	private AtlasService actuator;
	private String methodName;
	private int value;

	public AtomicAction(String name, AtlasService actuator, String methodName, int value, ReactivityEngine env) {
		super(name);
		this.actuator = actuator;
		this.methodName = methodName;
		this.value = value;
		this.env = env;
	}
	
	@Override
	public void execute() {
		// Cheat here
		System.out.println("Move servo " + env.getActuatorRefMap().get(actuator).getProperty("Node-Id") + " by " + value);
		try {
			int move = ((value * 100) / 180) + 1;
			((HS322Servo) actuator).moveServo(move);
		} catch (NullPointerException ex) {
			System.out.println("Servo is not in the servo map!");
		} catch (Exception e) {
			System.out.println("Error moving servo " + env.getActuatorRefMap().get(actuator).getProperty("Node-Id") + " by " + value);
			e.printStackTrace();
		}
	}

	@Override
	public String getExpression() {
		return env.getActuatorRefMap().get(actuator).getProperty("Node-Id") + "(" + value + ")";
	}

}
